#include "nexttetrisbox.h"

NextTetrisBox::NextTetrisBox(QWidget *parent) : QWidget(parent)
{

    //初始化nextBlock
    for (int i = 0; i < COUNT; i++)
    {
        nextBlock.x[i] = -1;
        nextBlock.y[i] = -1;
    }
    nextBlock.centerX = -1;
    nextBlock.centerY = -1;
    nextBlock.ID = 0;

    //设置本“下一个”窗口的宽度和高度
    int w = Tetris::getNextWidth();
    int h = Tetris::getNextHeight();
    setFixedSize(w, h);
    setAutoFillBackground(true);
}


void NextTetrisBox::updateNextTetris(Tetris tetris)
{
    nextBlock = tetris.getNextBlock();
    for (int i = 0; i < COUNT; i++)
    {
        nextBlock.x[i] -= RESTX;
        nextBlock.y[i] += RESTY;
    }
    //重新绘制
    repaint();
}


void NextTetrisBox::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    QPen pen;
    QBrush brush;


    //绘制nextBlock中的内容
    for (int i = 0; i < COUNT; i++)
    {
        if(nextBlock.ID==0)
        {
        brush.setColor(QColor(255, 100, 200));
        pen.setColor(QColor(255, 100, 200));
        }
        if(nextBlock.ID==1)
        {
            brush.setColor(QColor(0, 255, 255));
            pen.setColor(QColor(0, 255, 255));
        }
        if(nextBlock.ID==2)
        {
            brush.setColor(QColor(255, 255, 0));
            pen.setColor(QColor(255, 255, 0));
        }
        if(nextBlock.ID==3)
        {
            brush.setColor(QColor(0, 0, 255));
            pen.setColor(QColor(0, 0, 255));
        }
        if(nextBlock.ID==4)
        {
            brush.setColor(QColor(0, 255, 0));
            pen.setColor(QColor(0, 255, 0));
        }
        if(nextBlock.ID==5)
        {
            brush.setColor(QColor(200, 0, 0));
            pen.setColor(QColor(200, 0, 0));
        }
        if(nextBlock.ID==6)
        {
            brush.setColor(QColor(0, 200, 200));
            pen.setColor(QColor(0, 200, 200));
        }
        pen.setStyle(Qt::SolidLine);
        brush.setStyle(Qt::SolidPattern);
        painter.setPen(pen);
        painter.setBrush(brush);
        int x = nextBlock.x[i];
        int y = nextBlock.y[i];
        int x1 = x * WIDTH + x * INTERVAL;
        int y1 = y * HEIGHT + y * INTERVAL;
        painter.drawRect(x1, y1, WIDTH, HEIGHT);
    }
}
